package com.comdo.newup_loan.module.home;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.util.TitleMenuUtil;

public class ADWebviewActivity extends AppCompatActivity {

	private WebView webView;
	private String url = "";
	private String title = "";
	private ProgressDialog dialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
		url=getIntent().getStringExtra("url");
		title=getIntent().getStringExtra("title");
		new TitleMenuUtil(ADWebviewActivity.this,title,false);
		webView = (WebView) findViewById(R.id.webView);
		WebSettings wSet = webView.getSettings();
		wSet.setJavaScriptEnabled(true);
		webView.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式

	//	wSet.setUseWideViewPort(true);//设定支持viewport
	//	wSet.setLoadWithOverviewMode(true);
	//	wSet.setBuiltInZoomControls(true);
	//	wSet.setSupportZoom(true);//设定支持缩放
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			wSet.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
		}
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) { 
				// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				if (dialog == null || !dialog.isShowing()) {
					dialog = new ProgressDialog(ADWebviewActivity.this);
					dialog.setMessage("页面加载中，请稍后..");
				}
				dialog.show();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				if (dialog != null) {
					dialog.dismiss();
				}
			}
		});
		webView.loadUrl(url);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webView.canGoBack()) {
				webView.goBack();
				return false;
			} else {
				ADWebviewActivity.this.finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
